package com.example.toolproject.demo.controller;

import com.alibaba.fastjson.JSONObject;
import com.example.toolproject.demo.domain.Title;
import com.example.toolproject.demo.domain.User;
import com.example.toolproject.demo.domain.Video;
import com.example.toolproject.demo.service.HomePageService;
import com.example.toolproject.demo.util.FileUploadAndDownload;
import com.example.toolproject.demo.util.ResultBean;
import com.example.toolproject.demo.util.jwt.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;

/**
 * @ClassName HomePageController
 * @Description TODO    首页新闻内容配置  （是对欢迎页  index的上方本来静态内容的配置内容配置）
 * @Author lvyongwei
 * @Date 2020-08-10 15:36
 **/
@Controller
@CrossOrigin
@RequestMapping("/homePage")
public class HomePageController {

    @Autowired
    private FileUploadAndDownload fileUploadAndDownload;

    @Autowired
    private HomePageService homePageService;

    /**
     * 进行视频的修改
     * @param videoFile
     * @param videoId
     * @param request
     * @return
     */
    @PostMapping("/updateVideo")
    @ResponseBody
    public ResultBean updateVideo(@RequestParam("file") MultipartFile videoFile, Integer videoId, HttpServletRequest request){
        /**
         * 获取token
         */
        String token = request.getHeader("Authorization");
        String jwt = token.substring(token.lastIndexOf(" ")+1);
        User u = JwtUtil.unsign(jwt,User.class);
        if(u == null){
            return ResultBean.error("token无效");
        }
        /**
         * 创建video对象
         */
        Video video=new Video();
        String videoFilePath=fileUploadAndDownload.upload(videoFile,"\\video");
        video.setVideoPath("\\video\\"+videoFilePath);
        if(videoId==1){
            video.setVideoId(1);
        }else if(videoId==2){
            video.setVideoId(2);
        }
        try{
            homePageService.updateVideo(video);
            return ResultBean.success("修改成功");
        }catch (Exception e){
            return ResultBean.error("修改失败");
        }
    }

    /**
     * 获取到本地视频
     * @param request
     * @return
     */
    @GetMapping("/getIndexVideo")
    @ResponseBody
    public ResultBean getIndexVideo(HttpServletRequest request){
        return ResultBean.success("获取到视频信息",homePageService.getIndexVideo());
    }

    /**
     * 获取到标题内容
     * @param request
     * @return
     */
    @PostMapping("/getTitle")
    @ResponseBody
    public ResultBean getTitle(HttpServletRequest request,@RequestBody String getTitle){
        JSONObject jsonObject=JSONObject.parseObject(getTitle);
        int titleId= jsonObject.getInteger("titleId");
        try{
            Title title=homePageService.getTitle(titleId);
            return ResultBean.success("获取到内容",title);
        }catch (Exception e){
            return ResultBean.error("未获取到内容");
        }
    }

    /**
     * 根据标题编号来修改内容
     * @param request
     * @param title
     * @return
     */
    @PostMapping("/updateTitle")
    @ResponseBody
    public ResultBean updateTitle(HttpServletRequest request, @RequestBody Title title){
        String token = request.getHeader("Authorization");
        String jwt = token.substring(token.lastIndexOf(" ")+1);
        User u = JwtUtil.unsign(jwt,User.class);
        if(u == null){
            return ResultBean.error("token无效");
        }
        try{
            homePageService.updateTitle(title);
            return ResultBean.success("修改成功");
        }catch (Exception e){
            return ResultBean.error("修改失败");
        }
    }
}